//
//  EyeTestResultViewController.swift
//  App_Vision
//
//  Created by Flutter on 2021/7/30.
//

import UIKit
import UICore
import App_Common
import SnapKit
import Standard
import Leaf
import RxCocoa
import RxSwift
import Toast

class EyeTestResultViewController: ViewController  {
    
   
    
    let nextEyeTrigger = PublishSubject<Eye>()
    
    var result: EyeTestResult?
    
    lazy var nextButton: UIButton = {
        UIButton(type: .custom)
            .leaf
            .titleText(title: "继续检测右眼", for: .normal)
            .titleColor(.white, for: .normal)
            .backgroundColor(.systemBlue)
            .corners(radius: 10)
            .instance
    }()
    
    lazy var finishButton: UIButton = {
        UIButton(type: .custom)
            .leaf
            .titleText(title: "结束检测", for: .normal)
            .titleColor(#colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1), for: .normal)
            .backgroundColor(#colorLiteral(red: 0.9490196078, green: 0.9490196078, blue: 0.9490196078, alpha: 1))
            .corners(radius: 10)
            .instance
    }()
    
}

// MARK: - Override
extension EyeTestResultViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavigationBar(title: String.localized("EyeTestResult.ViewController.Title"))
        
        
        setupUI()
        
    }
    
}

// MARK: - UI
private extension EyeTestResultViewController {
    
    func setupUI() {
        
        nextButton.leaf.add(to: view).snp.makeConstraints {
            $0.centerX.equalToSuperview()
            $0.leading.equalToSuperview().offset(20)
            $0.trailing.equalToSuperview().offset(-20)
            $0.height.equalTo(40)
            $0.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(200)
        }
        
        if result?.leftVisionIndex == nil { 
            nextButton.setTitle("继续检测左眼", for: .normal)
        }else if result?.rightVisionIndex == nil {
            nextButton.setTitle("继续检测右眼", for: .normal)
        }
        
        
        nextButton.rx.controlEvent(.touchUpInside).subscribe {[weak self] event in
            if self?.result?.leftVisionIndex == nil {
                self?.nextEyeTrigger.onNext(.left)
            }else if self?.result?.rightVisionIndex == nil {
                self?.nextEyeTrigger.onNext(.right)
            }else {
                self?.view.makeToast("error", position: .center)
            }
            self?.navigationController?.popViewController(animated: false)

            
        }.disposed(by: disposeBag)
        
        if self.result?.leftVisionIndex != nil && self.result?.rightVisionIndex != nil {
            nextButton.isHidden = true
            finishButton.leaf.add(to: view).snp.makeConstraints {
                $0.edges.equalTo(nextButton)
            }
        }else {
            finishButton.leaf.add(to: view).snp.makeConstraints {
                $0.centerX.equalToSuperview()
                $0.leading.equalToSuperview().offset(20)
                $0.trailing.equalToSuperview().offset(-20)
                $0.height.equalTo(40)
                $0.top.equalTo(nextButton.snp.bottom).offset(20)
            }
            
        }
        
        
        finishButton.rx.controlEvent(.touchUpInside).subscribe {[weak self] event in
            
            self?.navigationController?.popToRootViewController(animated: true)
        
        }.disposed(by: disposeBag)
        
        
    }
    
}
